/*
	by Ilija
*/


#include "Audio.h"
#include "GLEngine.h"
#include "GLClient.h"

#ifdef ENGINE_FMOD

static CAudio* g_audio = NULL;

FMOD_RESULT F_CALLBACK mycallback(FMOD_CHANNEL *channel, FMOD_CHANNEL_CALLBACKTYPE type, void *commanddata1, void *commanddata2)
{
    int ch;
    FMOD_Channel_GetIndex(channel, &ch);
    CAudioSound* snd = g_audio->getSoundFromChannel(ch);
    if (type == FMOD_CHANNEL_CALLBACKTYPE_END && snd)
    {
        GLEvent e;
        e.type = EVT_SoundEnd;
        e.sound.channel = ch;
        e.sound.id = snd->id;
        g_audio->getEngine()->sendEvent(e);
    }
    return FMOD_OK;
}


CAudio::CAudio(GLEngine* engine, int sound_count) : m_Sounds(sound_count)
{
    const int CHANNELS = 100;

	FMOD_RESULT result;
	m_Enabled = false;
	result = FMOD_System_Create(&m_SoundSystem);
	if (result != FMOD_OK)
	{
		return;
	}
	result = FMOD_System_Init(m_SoundSystem, CHANNELS, FMOD_INIT_NORMAL, 0);
	if (result != FMOD_OK)
	{
		return;
	}

	m_Music.id = -1;
	m_Music.fsound = NULL;
	m_Music.fchannel = NULL;

	m_ChannelTracker = new CAudioSound* [CHANNELS];
	for (int i = 0; i < CHANNELS; i++)
        m_ChannelTracker[i] = NULL;

    m_Engine = engine;
	m_Enabled = true;
	g_audio = this;
}

CAudio::~CAudio()
{
    for (int i = 0; i < m_Sounds.size(); i++)
    {
        FMOD_Sound_Release(m_Sounds[i].fsound);
    }
	FMOD_System_Release(m_SoundSystem);
}

void CAudio::loadSound(const char* file_name, int id)
{
    m_Sounds[id].id = id;
    m_Sounds[id].fchannel = NULL;
	FMOD_RESULT res = FMOD_System_CreateSound(m_SoundSystem, file_name, FMOD_DEFAULT, 0, &m_Sounds[id].fsound);
    if (res == FMOD_OK)
        printf("NEW RESOURCE: Sound '%s'\n", file_name);
    else
        printf("NOT FOUND: Sound '%s'\n", file_name);
}

void CAudio::playSound(int id, int flags)
{
	int ch;
	FMOD_RESULT res = FMOD_System_PlaySound(m_SoundSystem, FMOD_CHANNEL_FREE, m_Sounds[id].fsound, false, &m_Sounds[id].fchannel);
	if (res == FMOD_OK)
    {
        FMOD_Channel_GetIndex(m_Sounds[id].fchannel, &ch);
        m_ChannelTracker[ch] = &m_Sounds[id];
    }
    else
        printf("Can not play %i\n", id);
}

void CAudio::playStream(const char* file_name, int flags)
{
}

void CAudio::playMusic(const char* file_name)
{
    if (m_Music.fsound)
        FMOD_Sound_Release(m_Music.fsound);
    FMOD_System_CreateStream(m_SoundSystem, file_name, FMOD_LOOP_NORMAL, 0, &m_Music.fsound);
	FMOD_System_PlaySound(m_SoundSystem, FMOD_CHANNEL_FREE, m_Music.fsound, false, &m_Music.fchannel);
}

void CAudio::playMusic()
{
    if (m_Music.fsound)
        FMOD_System_PlaySound(m_SoundSystem, FMOD_CHANNEL_FREE, m_Music.fsound, false, &m_Music.fchannel);
}

void CAudio::stopMusic()
{
    FMOD_Channel_Stop(m_Music.fchannel);
}

void CAudio::update()
{
	FMOD_System_Update(m_SoundSystem);
}

CAudioSound* CAudio::getSoundFromChannel(int ch)
{
    return m_ChannelTracker[ch];
}

#else

CAudio::CAudio(GLEngine* engine, int sound_count) { m_Engine = engine; m_Enabled = false; }
CAudio::~CAudio() { }
void CAudio::loadSound(const char* file_name, int id) { }
void CAudio::playSound(int id, int flags) { }
void CAudio::playStream(const char* file_name, int flags) { }
void CAudio::playMusic(const char* file_name) { }
void CAudio::playMusic() { }
void CAudio::stopMusic() { }
void CAudio::update() { }
CAudioSound* CAudio::getSoundFromChannel(int ch) { return NULL; }

#endif



